<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.searchKey"
        placeholder="请输入关键词"
        style="width: 200px;margin-right: 20px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
    <!-- <el-input v-model="Pages.readName" placeholder="开始时间"> </el-input> -->
        <el-date-picker
                v-model="listQuery.startTime"
                type="datetime"
                placeholder="开始时间">
        </el-date-picker>

        <el-date-picker
            v-model="listQuery.endTime"
            type="datetime"
            placeholder="结束时间">
        </el-date-picker>

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-refresh"
        @click="handleFilter"
      >
        刷新
      </el-button>
      <el-button
        v-has="'timu:add'"
        v-waves
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        {{ $t('table.add') }}
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      row-key="id"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
    <!-- 前面的勾选按钮 -->
     <el-table-column
      type="selection"
      width="55"
      header-align="center"
      align="center"
      >
    </el-table-column>

    <el-table-column
      prop="id"
      label="序号"
      width="80"
      align="center"
      header-align="center"
     >
    </el-table-column>

	<!-- 排除ID主键 -->
      <!-- <el-table-column :label="tempDesc.uuid" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.uuid }}</span>
        </template>
      </el-table-column> -->
      <el-table-column :label="tempDesc.timu" width="650px" show-overflow-tooltip>
        <template slot-scope="scope">
          <!-- <span>{{ scope.row.timu }}</span> -->
          <el-collapse >
            <el-collapse-item :title="scope.row.timu" name="1">
              <div class="my-line"></div>
              <!-- 添加选项按钮 -->
              <el-row>
                <el-col :span="24">
                  <el-button size="mini" type="primary" @click="addXuanxiang(scope.row.id)">添加选项</el-button>
                </el-col>
              </el-row>
               <!-- 展示选项列表 -->
              <el-row class="top-myline">
                <el-col :span="4">
                    <el-button type="primary" size="mini" icon="el-icon-edit" @click="addXuanxiang(scrop)" circle></el-button>
                    <el-button type="danger" size="mini" icon="el-icon-delete" circle></el-button>
                </el-col>
                <el-col :span="20">与现实生活一致：与现实生活的流程、逻辑保持一致，遵循用户习惯的语言和概念；</el-col>
              </el-row>
             <!-- 展示选项列表 -->
             <el-row class="top-myline">
              <el-col :span="4">
                  <el-button type="primary" size="mini" icon="el-icon-edit" circle></el-button>
                  <el-button type="danger" size="mini" icon="el-icon-delete" circle></el-button>
              </el-col>
               <el-col :span="20"> 在界面中一致：所有的元素和结构需保持一致，比如：设计样式、图标和文本、元素的位置等。</el-col>
             </el-row>

            </el-collapse-item>
          </el-collapse>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.username" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.username }}</span>
        </template>
      </el-table-column>



      <el-table-column :label="tempDesc.multiType" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-if="scope.row.multiType == 1">单选</span>
          <span v-if="scope.row.multiType == 2">多选</span>
        </template>
      </el-table-column>

      <!-- <el-table-column :label="tempDesc.multiTypeShow" width="150px" show-overflow-tooltip>
        <template slot-scope="scope">
            <el-button type="success" round @click="showXuanxiang(scope.row.id)">展开选项</el-button>
        </template>
      </el-table-column> -->
      <el-table-column :label="tempDesc.createTime" width="180px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="tempDesc.updateTime" width="180px" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>



      <!-- 这里是操作按钮，和权限相关 -->
      <el-table-column v-if="checkBtnPermission(['timu:edit','timu:delete','timu:generateQR'])" :label="$t('table.actions')" align="center" width="310" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-has="'timu:edit'" type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-has="'timu:delete'" type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">
            {{ $t('table.delete') }}
          </el-button>

        </template>
      </el-table-column>
    </el-table>













    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 編輯窗口 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :before-close="handleClose">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="120px"
        style="width: 600px; margin-left:50px;"
      >
        <!-- <el-form-item :label="tempDesc.uuid" prop="uuid">
          <el-input v-model="temp.uuid" :placeholder="'请输入' + tempDesc.uuid" />
        </el-form-item> -->
        <el-form-item :label="tempDesc.timu" prop="timu" >
          <!-- <el-input v-model="temp.timu" :placeholder="'请输入' + tempDesc.timu" /> -->
          <el-input type="textarea"   maxlength="2000"   show-word-limit :rows="4" :placeholder="'请输入' + tempDesc.timu"  v-model="temp.timu"></el-input>
        </el-form-item>
       <!-- <el-form-item :label="tempDesc.username" prop="username">
          <el-input v-model="temp.username" :placeholder="'请输入' + tempDesc.username" />
        </el-form-item> -->
        <el-form-item :label="tempDesc.multiType" prop="multiType">
            <template>
              <div>
                <el-radio v-model="temp.multiType" label="1" border>单选</el-radio>
                <el-radio v-model="temp.multiType" label="2" border>多选</el-radio>
              </div>
            </template>
          <!-- <el-input v-model="temp.multiType" :placeholder="'请输入' + tempDesc.multiType" /> -->
        </el-form-item>




     <!--   <el-form-item  prop="">
          <template>
            <div>
              <el-button type="success" round>成功按钮</el-button>
            </div>
            </template>
        </el-form-item> -->



      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisible = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>




    <!-- 新增选项/选项編輯窗口 -->
    <el-dialog :title="textMap[dialogStatusXX]" :visible.sync="dialogFormVisibleXX" :before-close="handleClose">
      <el-form
        ref="dataFormXX"
        :rules="rulesXX"
        :model="tempXX"
        label-position="right"
        label-width="120px"
        style="width: 600px; margin-left:50px;"
      >

        <el-form-item :label="tempDescXX.xuanxiang" prop="xuanxiang" >
          <!-- <el-input v-model="temp.timu" :placeholder="'请输入' + tempDesc.timu" /> -->
          <el-input type="textarea"   maxlength="200"   show-word-limit :rows="4" :placeholder="'请输入' + tempDescXX.xuanxiang"  v-model="tempXX.xuanxiang"></el-input>
        </el-form-item>

        <!-- 排序 -->
         <el-form-item :label="tempDescXX.order" prop="order" >
           <el-input type="input"     :placeholder="'请输入' + tempDescXX.right"  v-model="tempXX.order"></el-input>

         </el-form-item>

        <el-form-item :label="tempDescXX.right" prop="right">
            <template>
              <div>
                 <el-radio-group v-model="tempXX.right">
                  <el-radio   :label="1" border>正确</el-radio>
                  <el-radio   :label="2" border>错误</el-radio>
                 </el-radio-group>
              </div>
            </template>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="dialogFormVisibleXX = false">
          {{ $t('table.cancel') }}
        </el-button>
        <el-button type="primary" @click="dialogStatusXX==='create'?createDataXX():updateDataXX()">
          {{ $t('table.confirm') }}
        </el-button>
      </div>
    </el-dialog>





  </div>
</template>

<script>
import { checkBtnPermission } from '@/utils/permission' // 权限判断函数
import { fetchList, createRow, updateRow, deleteRow, generateQRUrl } from '@/api/choujiang/wx-timu'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import dateToString from '@/utils/DateTimeToString'

// 常量title
const tempDescXX = {
  	'id': '主键',
  	'uuid': 'uuid',
  	'xuanxiang': '选项',
  	'order': '排序',
  	'right': '是否正确',

}
// 常量title
const tempDesc = {
  	'id': '主键',
  	'uuid': 'uuid',
  	'timu': '题目',
  	'username': '录入账号',
  	'multiType': '单选/多选',
  	'createTime': '创建时间',
  	'updateTime': '更新时间',


}
export default {
  // name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {},
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        searchKey: '',
        startTime: '',
        endTime: '',
      },
      showReviewer: false,
      temp: {
  	     'id': '',
  	     'uuid': '',
  	     'timu': '',
  	     'username': '',
  	     'multiType': null,
  	     'createTime': '',
  	     'updateTime': '',
      },
      tempDesc: tempDesc,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '增加'
      },
      rules: {
         uuid: [{ required: true, message: '请输入' + tempDesc.uuid, trigger: 'blur' }],
         timu: [{ required: true, message: '请输入' + tempDesc.timu, trigger: 'blur' }],
         username: [{ required: true, message: '请输入' + tempDesc.username, trigger: 'blur' }],
         //multiType: [{ required: true, message: '请输入' + tempDesc.multiType, trigger: 'blur' }],
        //name: [{ required: true, message: '请输入' + tempDesc.name, trigger: 'blur' }],
        //account: [{ required: true, message: '请输入' + tempDesc.account, trigger: 'blur' }],
        //appid: [{ required: true, message: '请输入' + tempDesc.appid, trigger: 'blur' }],
        //appsecret: [{ required: true, message: '请输入' + tempDesc.appsecret, trigger: 'blur' }],
        //url: [{ required: true, message: '请输入' + tempDesc.url, trigger: 'blur' }],
        //token: [{ required: true, message: '请输入' + tempDesc.token, trigger: 'blur' }],
        //aeskey: [{ required: true, message: '请输入' + tempDesc.aeskey, trigger: 'blur' }]
      },

      //选项-----
      tempDescXX: tempDescXX,
      tempXX: {
  	     'id': '',
  	     'uuid': '',
  	     'xuanxiang': '',
  	     'order': 1,
         'right':2,
         'timu_id':null,
      },
      showXXlist:[], //仅仅作为展示
      rulesXX: {
         xuanxiang: [{ required: true, message: '请输入' + tempDescXX.xuanxiang, trigger: 'blur' }],
         order: [{ required: true, type: 'number', message: '请输入' + tempDescXX.order, trigger: 'blur' }],
      },
      dialogFormVisibleXX: false,
      dialogStatusXX: '',

    }
  },

  created() {
    this.getList()
  },
  methods: {
    checkBtnPermission,
    getList() {
      this.listLoading = true
      //判断时间是否为空
      this.listQuery.startTime = dateToString(this.listQuery.startTime);
      this.listQuery.endTime = dateToString(this.listQuery.endTime);
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetTemp() {
      this.temp = {
  	     'id': '',
  	     'uuid': '',
  	     'timu': '',
  	     'username': '',
  	     'multiType': '',
  	     'createTime': '',
  	     'updateTime': '',
        //'id': '',
        //'name': '',
        //'account': '',
        //'appid': '',
        //'appsecret': '',
        //'url': '',
        //'baseAPI': '',
        //'token': '',
        //'aeskey': ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick( () => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          createRow(this.temp).then( () => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateRow(tempData).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          var params = { 'id': row.id }
          deleteRow(params).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
    },
    generateQRUrl(row) {
      this.$confirm('确定生成二维码, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const tempData = Object.assign({}, row)
        generateQRUrl(tempData).then(() => {
          this.getList()
          this.$notify({
            title: '成功',
            message: '生成二维码成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    //提示关闭编辑窗口
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    load(tree, treeNode, resolve) {
            setTimeout(() => {
              resolve([
                {
                  id: 31,
                  date: '2016-05-01',
                  name: '王小虎',
                  address: '上海市普陀区金沙江路 1519 弄'
                }, {
                  id: 32,
                  date: '2016-05-01',
                  name: '王小虎',
                  address: '上海市普陀区金沙江路 1519 弄'
                }
              ])
        }, 1000)
    },



  //选项部分开始
    showXuanxiang(){//点击弹出选项数据

    },
    addXuanxiang(id){
      console.log("add: faterId: " + id);
      //this.tempXX = Object.assign({}, row) // copy obj
      this.dialogStatusXX = 'create'
      this.dialogFormVisibleXX = true
      this.$nextTick(() => {
        this.$refs['dataFormXX'].clearValidate()
      })
      this.tempXX.timu_id = id;
      console.log(JSON.stringify(this.tempXX));
    },

    createDataXX(){
      //提交到选项中
      
    },
    updateDataXX(){
      console.log("更新");
    }

  }

}
</script>
<style scoped>
  /deep/ .el-collapse-item__header{
    border-bottom: none !important;
  }
  /deep/ .el-collapse{
    border-top: none !important;
  }
  .my-line{
    border-bottom: 0.5px solid #dadada;
  }
  .top-myline{
    margin-top: 2px;
  }
</style>
